<extend name="Common/layout" />

<block name="title"><title>攻略管理</title></block>

<block name='css'>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/css/team.css">
</block>

<block name="content">

    <!--content-->
    <div class="col-sm-offset-2">

        <div class="panel panel-default">
            <div class="panel-body" style="height:70px;">
                <div class="form-group">
                    <div class="col-sm-3">
                        <div class="input-group">
                            <input class="form-control" type="text" id="route_name" placeholder="攻略名称">
                           <span class="input-group-btn">
                               <button class="btn btn-default">搜索</button>
                            </span>
                        </div>
                    </div>
                    <!--<div class="col-sm-2 ">-->
                        <!--<select class="form-control" id="type_select" onchange="fetchData()">-->
                            <!--<option value="0">全部</option>-->
                            <!--<option value="1">有效攻略</option>-->
                            <!--<option value="2">过期攻略</option>-->
                        <!--</select>-->
                    <!--</div>-->
                    <div class="col-sm-2 ">
                        <select class="form-control" id="status" onchange="fetchData()">
                            <option value="1">官网展示</option>
                            <option value="0">官网不展示</option>
                        </select>
                    </div>
                    <div class="col-sm-1 col-sm-offset-5">
                        <a href="{:U('Admin/Strategy/addStrategyPage')}"><button class="btn btn-primary " href="">添加攻略</button></a>
                    </div>
                </div>
            </div>
        </div>


        <table class="table table-bordered" id="table-route">
            <thead>
            <tr>
                <th style="width: 25px;">&nbsp;</th>
                <th style="width: 100px;">路线名称</th>
                <th style="width: 100px;">录入时间</th>
                <th style="width: 100px;">最后修改时间</th>
                <th style="width: 100px;">操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td><input type="checkbox" value=""></td>
                <td></td>
                <td></td>
                <td></td>
                <td><a href=""><span>查看攻略</span></a>&nbsp;<a href=""><span>编辑</span></a>&nbsp;<a href=""><span>删除</span></a></td>
            </tr>
            </tbody>
        </table>
        <div class="ml-9">
            <h4 id="totalNums"></h4>
            <input type="checkbox" value="" id="allChecked" class="allChecked"><span class="ml-9">全选</span>
            <button class="btn btn-warning btn-xs ml-9" onclick="javascript: confirmDialog() ;">删除</button>
        </div>

        <ul class="pagination" id="pagination">
            <!-- pages -->
        </ul>

    </div>

    <!-- template_tr start -->
    <table id="template-list" style="display:none;">
        <tbody id="template-list-tbody">
        <tr id="template-list-tr">
            <td><input type="checkbox" value="id"></td>
            <td>routename</td>
            <td>date</td>
            <td>date</td>
            <td>
                <a class="lookup"><span class="btn-link">查看攻略</span></a>
                <a class="edit"><span class="btn-link">编辑</span></a>
                <a class="del"><span class="btn-link">删除</span></a>
            </td>
        </tr>
        </tbody>
    </table>
    <input type="hidden" value="0" id="modalType"/>

    <div class="modal fade" id="showMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal_wrapper">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                        </button>
                        <h4 class="modal-title" id="myModalLabel_edit">攻略详情</h4>
                    </div>
                    <div class="modal-body">
                        <iframe src="" frameborder="0" id="routeMessage" style="width: 100%; height: 450px"></iframe>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</block>

<block name="js">

    <script type="text/javascript">

        var fetchData = function(furl){
            if(furl==null){
                url = "{:U('Strategy/getStrategyList')}";
            }else{
                url = furl;
            }

            $.ajax({
                url:url,
                type:'GET',
                dataType:'json',
                data:{
                    'title' :   $('#route_name').val(),
                    'type'   :   0,
                    'status' :  $('#status').val()
                }, success:function(json){
                    if(json.state!='1'){
                        alert("出错了");
                        return;
                    }

                    var show = json.show;
                    if(show == "" || show.total == 0){
                        $('#table-route tbody').html('<tr><td colspan="7" align="center">没有找到记录!!</td></tr>');
                        $('.pagination').html("");
                        $('#totalNums').html("");
                        return;
                    }else{
                        //列表渲染
                        var list = json.data;
                        var $template_list_tbody = $('#template-list tbody');
                        var $tds = $('#template-list td');
                        var template_render = '';

                        for(var i in list){

                            $tds[0].value = list[i].id;
                            $tds[1].innerHTML = list[i].name;
                            $tds[2].innerHTML = list[i].starttime;
                            $tds[3].innerHTML = list[i].endtime;
                            $('#template-list td .lookup').attr('href','javascript: var id ='+list[i].id+';openModal(id);');
                            $('#template-list td .del').attr('href','javascript: var id ='+list[i].id+';confirmDialog(id);');
                            $('#template-list td .edit').attr('href','{:U(\'Strategy/editStrategy\')}?id='+list[i].id);
                            $('#template-list td input').val(list[i].id);
                            $('#template-list td a').val(list[i].id);

                            template_render += $template_list_tbody.html();
                        }

                        $('#table-route tbody').html(template_render);
                        $('#totalNums').html('共'+json.show.total+'条攻略');
                    }
                    make_page(json.show,'.pagination','?');
                }
            })
        }

        fetchData();

        function confirmDialog(ids){
            var cd = confirm("确认删除？");
            if(cd == true){
                deleteStrategy(ids);
            }else{
                return;
            }
        }

        function deleteStrategy(ids){
            if(ids==null) {
                var ids = new Array();
                var selected = $('#table-route input:checkbox:checked');
                for (var i = 0; i < selected.length; i++) {
                    ids[i] = selected[i].value;
                }
            }

            $.ajax({
                url:"{:U('Strategy/deleteStrategy')}",
                type:'GET',
                dataType:'json',
                data:{
                    'ids':ids
                },success:function(json){
                    if(json=="success"){
                        fetchData();
                    }else if(json =="fail"){
                        alert("数据出错");
                    }else{
                        alert("删除失败");
                    }
                }
            });
        }

        $('#route_name').on("keyup",function(){
            fetchData();
        });

        function openModal(id){

            $('#routeMessage').attr('src',"{:U('Admin/Strategy/StrategyMessage')}?id="+id);
            $('#showMessage').modal().css({
                width: "100%",
                height: "100%"
            });
            $('#showMessage').modal({ show: true, backdrop: 'static' });

        }
    </script>

</block>